package com.itita.ww2.model.building;

import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.game.user.UserBuildingManager.BuildingType;
import com.itita.ww2.model.user.UserProfile;

/**
 * 用户建筑(基地)
 * @author NorthLan
 *
 */
public class BuildingBase {
	// private static final Logger logger = LoggerFactory.getLogger(BuildingBase.class);
	// 
	protected UserProfile userProfile;
	protected int uid;
	protected int level;
	protected String code;
	protected BuildingType type;

	public BuildingBase(UserProfile userProfile, String code, int level) {
		this.userProfile = userProfile;
		this.uid = userProfile.getUid();
		this.code = code;
		this.type = BuildingType.codeValueOf(code);
		this.level = level;
	}

	public void levelUp() {
		this.level++;
	}

	public int getLevel() {
		return level;
	}

	public String getCode() {
		return code;
	}

	public BuildingType getType() {
		return type;
	}

	public ISFSObject toSFSObject() {
		SFSObject ret = SFSObject.newInstance();
		ret.putInt("lv", this.level);
		return ret;
	}

	/**
	 * 从JSON数据中实例化对象
	 */
	public static BuildingBase newFromJsonData(UserProfile userProfile, String code, ISFSObject data) {
		BuildingType type = BuildingType.codeValueOf(code);
		BuildingBase ret = null;
		int level = data.getInt("lv");
		switch (type) {
		case BARRACK:
			ret = new BDBarracks(userProfile, code, level);
			break;
		case FORTIFICATIONS_INSTITUTE:
			ret = new BDFortification(userProfile, code, level);
			break;
		case BANK:
		case OILFIELD:
		case POWER_PLANT:
		case MILLS:
			ret = new BDResource(userProfile, code, level);
			break;
		default:
			ret = new BuildingBase(userProfile, code, level);
			break;
		}
		return ret;
	}
}
